﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseEntities;
using BussinessLogic;
using DataAccess;

namespace ATMSystem_G5_HaUI.UC5
{
    public partial class TransferCash : System.Web.UI.UserControl
    {
        
        AccountBus AccBus = new AccountBus();
        CustomerDAO CusDAO = new CustomerDAO();
        CardDAO CardDAO = new CardDAO();
        LogBus LogBus = new LogBus();
        public static int ATMID = 1;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnEnter_Click1(object sender, EventArgs e)
        {
            if (pnl2.Visible == true)
            {
                CheckAccount();
            }

            if (pnl4.Visible == true)
            {
                CheckBalance();
                string CardNo1 = txtReceiveID.Text;
                lblReceiveName1.Text = CusDAO.GetCustomerName(CardNo1);
                lblReceiveID1.Text = txtReceiveID.Text;                
                lblAmount.Text = txtAmount.Text;
            }
        }

        protected void btn5_Click(object sender, EventArgs e)
        {
            
            if (pnl1.Visible == true) 
            {
                string CardNo = Session["CardNo"].ToString();
                pnl2.Visible = true;
                pnl1.Visible = false;            
                lblTransferName.Text = CusDAO.GetCustomerName(CardNo);
                lblTransferID.Text = CardNo;
            }          
            else if (pnl3.Visible == true)
            {
                pnl2.Visible = false;
                pnl3.Visible = false;
                pnl4.Visible = true;
            }
            else if (pnl5.Visible == true)
            {
                string CardNo = Session["CardNo"].ToString();
                decimal a = Decimal.Parse(txtAmount.Text);
                AccBus.UpdateBalanceAccountReceive(a, txtReceiveID.Text);
                AccBus.UpdateBalanceAccountTransfer(a, CardNo);
                AddLogTransfer();
                AddLogReceive();
                pnl6.Visible = true;
                pnl5.Visible = false;
                pnl2.Visible = false;  
            }
            else if (pnl6.Visible == true)
            {
                pnl5.Visible = false;
                pnl2.Visible = false;            
                PrintReceiveRecipt();

            }
        }

        protected void btn6_Click(object sender, EventArgs e)
        {
            if (pnl1.Visible == true)
            {
                Response.Redirect("Home.aspx");
                Session["CardNo"] = null;
            }
            if (pnl3.Visible == true)
            {
                pnl3.Visible = false;
                pnl2.Visible = true;
                txtReceiveID.Text = "";
            }
            if (pnl5.Visible == true)
            {
                pnl5.Visible = false;
                pnl4.Visible = true;
                txtAmount.Text = "";
            }
            if (pnl6.Visible == true)
            {
                pnl5.Visible = false;
                pnl2.Visible = false;
                pnl6.Visible = false;
                Session["CardNo"] = null;
                Response.Redirect("Home.aspx");
            }
        }
        //cac ham su li
        public void AddLogTransfer()
        {
            string CardNo = Session["CardNo"].ToString();
            Log log = new Log();
            log.LogTypeID = 3;
            log.ATMID = ATMID;
            log.CardNo = CardNo;
            log.LogDate = DateTime.Now;
            log.Amount = Decimal.Parse(txtAmount.Text);
            log.Details = "Nhan chuyen tien cho: " + CusDAO.GetCustomerName(txtReceiveID.Text);
            LogBus.WriteLog(log);
        }
        public void AddLogReceive()
        {
            string CardNo = Session["CardNo"].ToString();
            Log log = new Log();
            log.LogTypeID = 3;
            log.ATMID = ATMID;
            log.CardNo = txtReceiveID.Text;
            log.LogDate = DateTime.Now;
            log.Amount = Decimal.Parse(txtAmount.Text);
            log.Details = "Nhan tien cua: " + CusDAO.GetCustomerName(CardNo);
            LogBus.WriteLog(log);
        }
        public void CheckBalance()
        {
            string CardNo = Session["CardNo"].ToString();
            decimal a = Decimal.Parse(txtAmount.Text);
            if (a % 50000 != 0)
            {
                lblS41.Text = "Số tiền chuyển phải là bội số của 50.000đ";
                txtAmount.Text = "";
            }
            else 
            {
                if (AccBus.CheckBalance(a, CardNo))
                {
                    pnl2.Visible = false;
                    pnl4.Visible = false;
                    pnl3.Visible = false;
                    pnl5.Visible = true;
                    lblS41.Text = "";
                    lblS42.Text = "";
                }
                else
                {
                    lblS41.Text = "Your account isn't enough to transfer!";
                    lblS42.Text = "Re-enter amount you want to transfer";
                    lblAmount.Text = "";
                }
            }
            
        }
        public void CheckAccount()
        {
            string CardNo = Session["CardNo"].ToString();
            string cardno = txtReceiveID.Text;
            if (CardDAO.CheckCardNo(cardno)== true)
            {
                if (cardno == CardNo)
                {
                    lblS21.Text = "You don't transfer for yourself!";
                    lblS22.Text = "Re-enter account you want to transfer";
                    lblS23.Text = "";
                    lblTransferID.Text = "";
                    lblTransferName.Text = "";
                    lblS24.Text = "press enter to continue";
                }
                else 
                {
                    pnl2.Visible = false;
                    pnl3.Visible = true;
                    lblReceiveName.Text = CusDAO.GetCustomerName(cardno);
                    lblReceiveID.Text = txtReceiveID.Text;
                }
                
            }
            else
            {
                lblS21.Text = "Your account not exist!";
                lblS22.Text = "Re-enter account you want to transfer";
                lblS23.Text = "";
                lblTransferID.Text = "";
                lblTransferName.Text = "";
                lblS24.Text = "press enter to continue";
            }
        }
        public void PrintReceiveRecipt()
        {
            pn7.Visible = true;
            pnl6.Visible = false;
            pnl2.Visible = false;
            if (pn7.Visible == true)
            {
                System.Threading.Thread.Sleep(2000);
                Response.Redirect("Home.aspx");
                Session["CardNo"] = null;
            }
            
        }

        //ham su li button số
        protected void num1_Click(object sender, EventArgs e)
        {
            if (pnl4.Visible == true)
            {
                txtAmount.Text += "1";
            }
            else if (pnl2.Visible == true)
            {
                txtReceiveID.Text += "1";
            }
        }

        protected void num2_Click(object sender, EventArgs e)
        {
            if (pnl4.Visible == true)
            {
                txtAmount.Text += "2";
            }
            else if (pnl2.Visible == true)
            {
                txtReceiveID.Text += "2";
            }
        }

        protected void num3_Click(object sender, EventArgs e)
        {
            if (pnl4.Visible == true)
            {
                txtAmount.Text += "3";
            }
            else if (pnl2.Visible == true)
            {
                txtReceiveID.Text += "3";
            }
        }

        protected void num4_Click(object sender, EventArgs e)
        {
            if (pnl4.Visible == true)
            {
                txtAmount.Text += "4";
            }
            else if (pnl2.Visible == true)
            {
                txtReceiveID.Text += "4";
            }
        }

        protected void num5_Click(object sender, EventArgs e)
        {
            if (pnl4.Visible == true)
            {
                txtAmount.Text += "5";
            }
            else if (pnl2.Visible == true)
            {
                txtReceiveID.Text += "5";
            }
        }

        protected void num6_Click(object sender, EventArgs e)
        {
            if (pnl4.Visible == true)
            {
                txtAmount.Text += "6";
            }
            else if (pnl2.Visible == true)
            {
                txtReceiveID.Text += "6";
            }
        }

        protected void num7_Click(object sender, EventArgs e)
        {
            if (pnl4.Visible == true)
            {
                txtAmount.Text += "7";
            }
            else if (pnl2.Visible == true)
            {
                txtReceiveID.Text += "7";
            }
        }

        protected void num8_Click(object sender, EventArgs e)
        {
            if (pnl4.Visible == true)
            {
                txtAmount.Text += "8";
            }
            else if (pnl2.Visible == true)
            {
                txtReceiveID.Text += "8";
            }
        }

        protected void num9_Click(object sender, EventArgs e)
        {
            if (pnl4.Visible == true)
            {
                txtAmount.Text += "9";
            }
            else if (pnl2.Visible == true)
            {
                txtReceiveID.Text += "9";
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            if (pnl4.Visible == true)
            {
                txtAmount.Text = "";
            }
            if (pnl2.Visible == true)
            {
                txtReceiveID.Text = "";
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }

        protected void numDot_Click(object sender, EventArgs e)
        {
            if (pnl4.Visible == true)
            {
                txtAmount.Text += ".";
            }
        }

        protected void num0_Click(object sender, EventArgs e)
        {
            if (pnl4.Visible == true)
            {
                txtAmount.Text += "0";
            }
        }

        protected void numSharp_Click(object sender, EventArgs e)
        {
            if (pnl4.Visible == true)
            {
                txtAmount.Text += "#";
            }
        }
    }
}